package com.taichuan.keysupport.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ScrollView;

/**
 * 该类主要用于
 *
 * @author CamelLuo
 * @version 2020/8/3
 */
public class TcScrollView extends ScrollView {
    private static final String TAG = TcScrollView.class.getSimpleName();
    private int curScrollDistance;

    public TcScrollView(Context context) {
        super(context);
    }

    public TcScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TcScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 定义一个方法用于控制Scroll上滑还是下滑
     * @param scrollUpOrDown
     */
    public void scroll(boolean scrollUpOrDown) {
        //计算可滑动的最大距离
        int scrollMaxDistance = this.getChildAt(0).getHeight() - this.getMeasuredHeight();
        if (scrollUpOrDown) {
            if (curScrollDistance > 0) {
                //每次滑动控件的1/2高度距离
                curScrollDistance = curScrollDistance - this.getMeasuredHeight() / 2;
                this.scrollTo(0, curScrollDistance);
            } else {
                curScrollDistance = 0;
                Log.i(TAG, "scroll: 已到顶部，无需滑动");
            }
        } else {
            if (curScrollDistance <= scrollMaxDistance) {
                curScrollDistance = curScrollDistance + this.getMeasuredHeight() / 2;//每次滑动控件的1/2高度距离
                this.scrollTo(0, curScrollDistance);
                Log.i(TAG,"滑动距离:" + curScrollDistance + "  滑动最大距离:" + scrollMaxDistance);
            } else {
                curScrollDistance = scrollMaxDistance;
                Log.i(TAG,"已到底部，无需滑动");
            }
        }
    }
}
